package com.htgd.gardenexpo.controller;


import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.*;
import com.htgd.gardenexpo.page.FoodQuery;
import com.htgd.gardenexpo.page.FoodRankQuery;
import com.htgd.gardenexpo.page.PageBase;
import com.htgd.gardenexpo.service.GeBaseUserService;
import com.htgd.gardenexpo.service.GeConFoodService;
import com.htgd.gardenexpo.service.IGeConFoodRuleService;
import com.htgd.gardenexpo.service.WxUserCollectService;
import com.htgd.gardenexpo.utils.SecurityParameter;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.utils.UniteUtils;
import com.htgd.gardenexpo.vo.GeConFoodRankingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 美食表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-08
 */
@RestController
@RequestMapping("/api/food")
@Api(tags = "美食")
@Slf4j
public class GeConFoodController {

    @Autowired
    private GeConFoodService geConFoodService;

    @Autowired
    private GeBaseUserService geBaseUserService;
    @Autowired
    private WxUserCollectService wxUserCollectService;

    @Autowired
    private IGeConFoodRuleService geConFoodRuleService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult add (@RequestBody GeConFood geConFood){
        log.info("美食-添加：{}",geConFood);
        return geConFoodService.add(geConFood);
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult edit (@RequestBody GeConFood geConFood){
        log.info("美食-编辑：{}",geConFood);
        return geConFoodService.edit(geConFood);
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult delete (int id){
        log.info("美食-删除：{}",id);
        return geConFoodService.delete(id);
    }


    @PostMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConFood>> list(@RequestBody FoodQuery foodQuery,
                                              HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        log.info("美食-列表查询：{}，{}",foodQuery,openId);
        PageInfo<GeConFood> list=geConFoodService.querylist(foodQuery,openId);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }

    @GetMapping(value = "/review")
    @ApiOperation("审核")
    public CommonResult review( GeConFood geConFood) {
        log.info("美食-审核：{}",geConFood);
        return geConFoodService.review(geConFood);
    }

    @PostMapping(value = "/details")
    @ApiOperation("详情")
    @SecurityParameter
    public CommonResult details(@RequestBody String s,
                                                    HttpServletRequest request){
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        log.info("美食-详情：{}，{}",openId,s);
        Integer id= JSON.parseObject(s).getInteger("id");
        Integer sign= JSON.parseObject(s).getInteger("sign");
        //点击加1
        if(sign!=null && sign ==1){
            addHit(String.valueOf(id));
        }
        GeConFood geConFood = geConFoodService.details(id,openId);
        return CommonResult.success("查询成功",geConFood);
    }

    @GetMapping("/addHit")
    @ApiOperation("点击量新增")
    public CommonResult<JSONObject> addHit(@RequestParam String id){
        CommonResult<JSONObject> result = new CommonResult<>();
        geConFoodService.addHit(id);
        result.setCode(200);
        result.setMessage("增加成功");
        return result;
    }

    @GetMapping("/getRanking")
    @ApiOperation("获取榜单")
    public CommonResult<List<GeConFoodRankingVO>> getRanking(FoodRankQuery foodRankQuery, HttpServletRequest request){
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        log.info("美食-榜单：{},{}",foodRankQuery,openId);
        //规则
        GeConFoodRule geConFoodRule = geConFoodRuleService.getById("1");
        //判断
        if(geConFoodRule.getStatus()==0){//关闭
            return CommonResult.success("成功",new ArrayList<>());
        }else {
            PageInfo<GeConFoodRankingVO> list=geConFoodService.getRanking(foodRankQuery,openId);
            return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
        }
    }
}
